﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;

namespace ScoreMS.Views
{
    /// <summary>
    /// CourseAdd.xaml 的交互逻辑
    /// </summary>
    public partial class CourseAdd : Page
    {
        public CourseAdd()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(CourseAdd_Loaded);
        }

        void CourseAdd_Loaded(object sender, RoutedEventArgs e)
        {
            lstClass.ItemsSource = BLL.ClassBS.GetClassList();
            lstPro.ItemsSource = BLL.Pro_emphasisBS.GetPro_emphasisList();
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtcourse_id.Text.Trim() == ""
                    || txtcourse_name.Text.Trim() == ""
                    || txtcourse_points.Text.Trim() == ""
                    || txtcourse_stutype.Text.Trim() == ""
                    || txtcourse_type.Text.Trim() == ""
                    || txtcourse_hours_x.Text.Trim() == ""
                    || txtcourse_hours_y.Text.Trim() == "")
                {
                    MessageBox.Show("请填写完整！", "提示");
                    return;
                }
                if (!rbnClass.IsChecked.HasValue && !rbnPro.IsChecked.HasValue) //两个RadioBox都没值
                {
                    MessageBox.Show("请在右边选择面向的班级或专业！", "提示");
                    return;
                }
                if (!BLL.CourseBS.AddCourse(new Model.Course()
                {
                    course_id = txtcourse_id.Text.Trim(),
                    course_name = txtcourse_name.Text.Trim(),
                    course_points = int.Parse(txtcourse_points.Text.Trim()),
                    course_stutype = txtcourse_stutype.Text.Trim(),
                    course_type = txtcourse_type.Text.Trim(),
                    course_hours_x = int.Parse(txtcourse_hours_x.Text.Trim()),
                    course_hours_y = int.Parse(txtcourse_hours_y.Text.Trim())
                }))
                {
                    MessageBox.Show("添加失败！请确保输入数据正确！", "警告");
                    return;
                }
                string course_id = txtcourse_id.Text.Trim();
                if (rbnClass.IsChecked == true)
                {
                    foreach (var item in lstClass.SelectedItems)
                    {
                        Model.Class model = item as Model.Class;
                        BLL.CourseBS.AddClass(course_id, model.class_id);
                    }
                }
                if (rbnPro.IsChecked == true)
                {
                    foreach (var item in lstClass.SelectedItems)
                    {
                        Model.Pro_emphasis model = item as Model.Pro_emphasis;
                        BLL.CourseBS.AddPro_emphasis(course_id, model.pro_emphasis_id);
                    }
                }
                MessageBox.Show("恭喜！添加成功！", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加失败！" + ex.ToString(), "警告");
            }
        }
    }

    [ValueConversion(typeof(bool), typeof(Visibility))]
    public sealed class VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException("VisibilityConverter.ConvertBack is not supported.");
        }
    }


}
